package com.antler.smt.admin.controller;

import com.antler.smt.admin.annotation.OperateCut;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.request.admin.PurchaserAccountReq;
import com.antler.smt.request.base.BasePageReq;
import com.antler.smt.response.admin.GetOrderListResp;
import com.antler.smt.response.admin.GetPurchaserCashCouponListResp;
import com.antler.smt.response.admin.GetPurchaserVipInfoListResp;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.admin.ManageVipService;
import com.antler.smt.service.admin.PurchaserService;
import com.antler.smt.utils.LogUtil;
import com.antler.smt.validate.DataValidator;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Arrays;
import java.util.List;

/**
 * 后台管理vip接口
 *
 * @author zhangyi
 * @date 2018/4/18 16:54
 */
@RestController
@RequestMapping("/vip")
@Api(description = "后台采购商管理列表接口")
public class PurchaserManageController {

    /**
     * 日志
     */
    private static Logger log = LogUtil.get();

    @Resource
    private ManageVipService manageVipService;

    @Resource
    private PurchaserService purchaserService;

    /**
     * 分类获取采购商列表
     *
     * @return
     */
    @GetMapping("/manageVipList")
    @ApiOperation(value = "采购商列表（全部：不传，未提交：0，待审核：1，已审核：2,3）", httpMethod = "GET")
    public Result<Page<GetPurchaserVipInfoListResp>> manageVipList(BasePageReq basePageReq,@RequestParam(value = "status") String list){

        Result result = null;

            String[] split = list.split(",");
            Integer[] len = new Integer[split.length];
            for (int i = 0; i < split.length; i++){
                len[i] = Integer.parseInt(split[i]);
            }
            //转换数组
            List<Integer> sl = Arrays.asList(len);
            result = manageVipService.manageVipList(basePageReq,sl);
        return result;
    }

    /**
     * 获取此采购商所有订单列表
     *
     * @param id 采购商的id信息
     * @return
     */
    @GetMapping("/orderCount")
    @ApiOperation(value = "获取此采购商所有订单列表", httpMethod = "GET")
    public Result<PageInfo<GetOrderListResp>> orderCount(BasePageReq request, @RequestParam(value = "id") Long id){

        DataValidator.isBlank(String.valueOf(id),"采购商id必传！");
        DataValidator.isNull(request,"分页实体不为空！");

        Result result = null;
            result = manageVipService.orderCount(id,request);
        return result;
    }

    /**
     * 冻结采购商账户
     *
     * @param id 采购商的id信息
     * @param status 用户状态信息
     * @return
     */
    @PostMapping("/freeze")
    @ApiOperation(value = "冻结采购商账户", httpMethod = "POST")
    @OperateCut(content = "冻结采购商账户")
    public Result<ResultCodeEnum> freeze(@RequestParam(value = "id") Long id,
                                         @RequestParam(value = "status") Integer status){

        DataValidator.isBlank(String.valueOf(id),"采购商id必传！");

        Result result = null;
            result = manageVipService.freeze(id,status);
        return result;
    }

    /**
     * 发放代金券
     *
     * @param id 采购商的id信息
     * @return
     */
    @PostMapping("/provideCashCoupon")
    @ApiOperation(value = "发放代金券", httpMethod = "POST")
    @OperateCut(content = "发放代金券")
    public Result<ResultCodeEnum> provideCashCoupon(@RequestParam(value = "id") Long id,
                                                    @RequestBody List<Integer> cashLists) throws ParseException {
        DataValidator.isBlank(String.valueOf(id),"采购商id必传！");
        Result result = null;
            result = manageVipService.provideCashCoupon(id,cashLists);
        return result;
    }

    /**
     * 显示所有代金券信息列表
     *
     * @return
     */
    @PostMapping("/showCashCoupon")
    @ApiOperation(value = "显示代金券信息列表", httpMethod = "POST")
    @OperateCut(content = "显示所有代金券列表")
    public Result showCashCoupon(){

        Result result = null;

            result = manageVipService.showCashCoupon();
        return result;
    }

    /**
     * 获取当前采购商的有效代金券列表
     *
     * @param id 采购商的id信息
     * @return
     */
    @GetMapping("/cashCouponInfo")
    @ApiOperation(value = "获取当前采购商的有效代金券列表", httpMethod = "GET")
    public Result<GetPurchaserCashCouponListResp> cashCouponInfo(@RequestParam(value = "id") Long id){

        DataValidator.isBlank(String.valueOf(id),"采购商id必传！");

        Result result = null;

            result = manageVipService.cashCouponInfo(id);
        return result;
    }

    /**
     * 作废代金券
     *
     * @param cashCouponId 此代金券的id
     * @return
     */
    @PostMapping("/cancelCashCoupon")
    @ApiOperation(value = "作废代金券", httpMethod = "POST")
    @OperateCut(content = "作废代金券")
    public Result<ResultCodeEnum> cancelCashCoupon(@RequestParam(value = "cashCouponId") Long cashCouponId){

        DataValidator.isBlank(String.valueOf(cashCouponId),"采购商代金券id必传");

        Result result = null;

            result = manageVipService.cancelCashCoupon(cashCouponId);
        return result;
    }

    /**
     * 采购商账户修改
     *
     * @param purchaserId 采购商的id
     * @return
     */
    @PostMapping("/editPurchaserAccount")
    @ApiOperation(value = "采购商账户修改（修改代金券数量，星级等等）", httpMethod = "POST")
    @OperateCut(content = "修改采购商账户信息")
    public Result<ResultCodeEnum> editPurchaserAccount(@RequestBody PurchaserAccountReq req,@RequestParam(value = "purchaserId") Long purchaserId){

        DataValidator.isBlank(String.valueOf(purchaserId),"采购商id不能为空！");
        DataValidator.isNull(req,"采购商账户实体必传");

        Result result = null;

            result = purchaserService.editPurchaserAccount(req,purchaserId);
        return result;
    }



}
